<?php

namespace partner\functions\wechat;

use Exception;
use partner\core\BaseWechatPartner;
use partner\functions\wechat\lib\BankAccountInfo;
use partner\functions\wechat\lib\ContactInfo;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Util\MediaUtil;
use partner\functions\wechat\lib\BaseWechatPartner as WechatPartner;

class partner extends BaseWechatPartner
{
    public function apply() 
    {
        $this->log("调用了: " . __METHOD__);
        $data = [];
        
        foreach ($this->app->params as $property => $val) {
            if ($val instanceof WechatPartner) {
                if (empty($data))
                    $data = $val;
                else
                    array_merge($data, $val);
            }
        }

        $this->log("调用apply接口的参数: " . json_encode($data, true));
        return '调用微信applyment接口';
    }

    public function uploadFile(string $file_path = '') 
    {
        if (empty($file_path) || ! file_exists($file_path))
            throw new Exception("待上传文件不存在");

        try {
            $media = new MediaUtil($file_path);

            $resp = $this->builder->chain('v3/merchant/media/upload')
                ->post([
                    'body' => $media->getStream(),
                    'headers' => [
                        'Accept' => 'application/json',
                        'Content-Type' => $media->getContentType()
                    ]
                ]);
            $this->log("上传{$file_path}成功, http_code: {$resp->getStatusCode()}");
            return $resp->getBody();
        } catch(Exception $e) {
            $this->log("上传{$file_path}失败了, 失败原因: {$e->getMessage()}");
            throw new Exception('图片上传失败');
        }
    }
}